﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.List {
    public class IntersectNode : FuncNode {

        public IntersectNode()
            : base() {
            this.Value = "intersect";
        }

        public override object Evaluate() {
            ListNode result = new ListNode();

            for (int i = 0; i < Parameters.Count; i++) {
                ListNode c = (ListNode)Parameters[i];
                foreach (Node u in c.Parameters) {
                    for (int j = i + 1; j < Parameters.Count; j++) {
                        ListNode d = (ListNode)Parameters[j];
                        foreach (Node v in d.Parameters) {
                            if (u.Value == v.Value) {
                                result.Parameters.Add(u);
                            }
                        }
                    }
                }
            }

            return result;
        }
    }
}
